//
//  TTGemstonesRightView.m
//  TaoTuo
//
//  Created by admin on 2019/8/22.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTGemstonesRightView.h"
#import "TTSearchStoneModel.h"
#import "TTStoneCollectionViewCell.h"

@interface TTGemstonesRightView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *oneSelectedArray;    //第一列 存放选中的item
@property (nonatomic, strong) NSMutableArray *twoSelectedArray;    //第一列 存放选中的item
@property (nonatomic, strong) NSMutableArray *threeSelectedArray;    //第一列 存放选中的item
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, copy) SelectedArrayBlock selectedArrayBlock;

@end

@implementation TTGemstonesRightView

- (instancetype)initWithFrame:(CGRect)frame dataArray:(NSArray *)dataArray selectArrayBlock:(SelectedArrayBlock)selectBlock
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.selectedArrayBlock = selectBlock;
        
        self.dataArray = dataArray;
        
        [self.collectionView reloadData];
        
        
    }
    return self;
}

- (void)resetView
{
    [self.oneSelectedArray removeAllObjects];
    [self.twoSelectedArray removeAllObjects];
    [self.threeSelectedArray removeAllObjects];
    [self.collectionView reloadData];
}

- (void)setSelectedWithIndexs:(NSArray<NSNumber *> *)indexs
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        for (NSNumber *num in indexs) {
            NSInteger index = num.integerValue;
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
            [self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
        }
        
        NSInteger one = indexs.firstObject.integerValue, two = [indexs[1] integerValue], three = indexs.lastObject.integerValue;
        if (self.selectedArrayBlock) {
            self.selectedArrayBlock(@[self.dataArray[one]], @[self.dataArray[two]], @[self.dataArray[three]]);
        }
        
    });
}

// 选中第一个
- (void)setSelectedFirstItem
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        TTSearchStoneModel *model = self.dataArray.firstObject;
        [self.oneSelectedArray addObject:model];
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
        
        if (self.selectedArrayBlock) {
            self.selectedArrayBlock(self.oneSelectedArray, nil, nil);
        }
    });
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TTStoneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    TTSearchStoneModel *model = self.dataArray[indexPath.row];
    
    if (indexPath.row % 3 == 0) {
        [self.oneSelectedArray addObject:model];
    } else if (indexPath.row % 3 == 1) {
        [self.twoSelectedArray addObject:model];
    }  else if (indexPath.row % 3 == 2) {
        [self.threeSelectedArray addObject:model];
    }
    
    if (self.selectedArrayBlock) {
        self.selectedArrayBlock(self.oneSelectedArray, self.twoSelectedArray, self.threeSelectedArray);
    }
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    TTSearchStoneModel *model = self.dataArray[indexPath.row];
    if (indexPath.row % 3 == 0) {
        [self.oneSelectedArray removeObject:model];
    } else if (indexPath.row % 3 == 1) {
        [self.twoSelectedArray removeObject:model];
    }  else if (indexPath.row % 3 == 2) {
        [self.threeSelectedArray removeObject:model];
    }
    
    if (self.selectedArrayBlock) {
        self.selectedArrayBlock(self.oneSelectedArray, self.twoSelectedArray, self.threeSelectedArray);
    }
}


- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(45, 45 * kScale);
        layout.minimumLineSpacing = 1;
        layout.minimumInteritemSpacing = 1;
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.allowsMultipleSelection = YES;  // 可多选
        [_collectionView registerClass:[TTStoneCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        [self addSubview:_collectionView];
        
    }
    return _collectionView;
}


- (NSMutableArray *)oneSelectedArray
{
    if (!_oneSelectedArray) {
        _oneSelectedArray = [NSMutableArray array];
    }
    return _oneSelectedArray;
}


- (NSMutableArray *)twoSelectedArray
{
    if (!_twoSelectedArray) {
        _twoSelectedArray = [NSMutableArray array];
    }
    return _twoSelectedArray;
}

- (NSMutableArray *)threeSelectedArray
{
    if (!_threeSelectedArray) {
        _threeSelectedArray = [NSMutableArray array];
    }
    return _threeSelectedArray;
}



@end
